package com.yundian.cloudme.service.impl;

import java.util.Date;
import java.util.List;

import com.opennice.tools.BTools;
import com.opennice.tools.Tools;
import com.yundian.car.dao.impl.CarDAO;
import com.yundian.cloudme.service.ICloudmeService;
import com.yundian.filebase.dao.impl.FilebaseDAO;
import com.yundian.fileorder.dao.impl.FileOrderDAO;
import com.yundian.member.dao.impl.MemberDAO;
import com.yundian.member.service.impl.MemberService;
import com.yundian.vo.Car;
import com.yundian.vo.Filebase;
import com.yundian.vo.Fileorder;
import com.yundian.vo.Member;

public class CloudmeService implements ICloudmeService {

	private FilebaseDAO filebaseDAO;
	private MemberDAO memberDAO;
	private CarDAO carDAO;
	private FileOrderDAO fileOrderDAO;
	public FilebaseDAO getFilebaseDAO() {
		return filebaseDAO;
	}
	public void setFilebaseDAO(FilebaseDAO filebaseDAO) {
		this.filebaseDAO = filebaseDAO;
	}
	
	
	@Override
	public void loadFile(String fileName, String url, int kind,Member member) {
		// TODO Auto-generated method stub
		Filebase filebase=new Filebase();
		filebase.setFileAddTime(new Date(System.currentTimeMillis()));
		filebase.setFileDescript("");
		filebase.setFileKind(kind);
		filebase.setFileState(0);
		filebase.setFilePrintsum(0);
		filebase.setFileUrl(url);
		filebase.setFileName(fileName);
		filebase.setMember(member);
		
		this.filebaseDAO.saveFilebase(filebase);
		if(kind==1)
			member.setMemberFilesum(member.getMemberFilesum()+1);
		else if(kind==2)
			member.setMemberImagesum(member.getMemberImagesum()+1);
		this.memberDAO.updateMember(member);
	}
	
	
	
	public CarDAO getCarDAO() {
		return carDAO;
	}
	public void setCarDAO(CarDAO carDAO) {
		this.carDAO = carDAO;
	}
	public FileOrderDAO getFileOrderDAO() {
		return fileOrderDAO;
	}
	public void setFileOrderDAO(FileOrderDAO fileOrderDAO) {
		this.fileOrderDAO = fileOrderDAO;
	}
	public MemberDAO getMemberDAO() {
		return memberDAO;
	}
	public void setMemberDAO(MemberDAO memberDAO) {
		this.memberDAO = memberDAO;
	}
	@Override
	public Car loadCarByMember(Member member){
		List carList=carDAO.browseCarByMemberIdAndStatu(member.getMemberId(), 0);
		if(carList.size()<=0){
			Car car=new Car();
			car.setCarCarName(BTools.getStringFromDate(new Date(System.currentTimeMillis()))+member.getMemberId());
		    car.setCarKind(1);
		    car.setCarShopid(0);
		    car.setCarTime(new Date(System.currentTimeMillis()));
		    car.setCarTotalprice(0.0);
		    car.setCarMemberid(member.getMemberId());
		    car.setCarPagesum(0);
		    car.setCarState(0);
		    carDAO.saveCar(car);
		}
		carList=carDAO.browseCarByMemberIdAndStatu(member.getMemberId(), 0);
		Car car=(Car) carList.get(0);
		return car;
	}
	@Override
	public void deleteOrderFileFromCar(int orderId) {
		// TODO Auto-generated method stub
		fileOrderDAO.deleteFileOrder(orderId);
	}
	@Override
	public Car addFileToCar(Member member, int  fileid) {
		// TODO Auto-generated method stub
		List carList=carDAO.browseCarByMemberIdAndStatu(member.getMemberId(), 0);
		if(carList.size()<=0){
			Car car=new Car();
			car.setCarCarName(BTools.getStringFromDate(new Date(System.currentTimeMillis()))+member.getMemberId());
		    car.setCarKind(1);
		    car.setCarShopid(0);
		    car.setCarTime(new Date(System.currentTimeMillis()));
		    car.setCarTotalprice(0.0);
		    car.setCarMemberid(member.getMemberId());
		    car.setCarPagesum(0);
		    car.setCarState(0);
		    carDAO.saveCar(car);
		}
		carList=carDAO.browseCarByMemberIdAndStatu(member.getMemberId(), 0);
		Car car=(Car) carList.get(0);
		Fileorder fileorder=new Fileorder();
		fileorder.setCar(car);
		fileorder.setOrderDoublepage(0);
		fileorder.setOrderIngbar(0);
		fileorder.setOrderIspage(1);
		fileorder.setOrderPrice(0.0);
		fileorder.setOrderPrintsum(1);
		fileorder.setOrderShopid(0);
		fileorder.setOrderState(0);
		fileorder.setOrderTime(new Date(System.currentTimeMillis()));
		
		Filebase filebase=filebaseDAO.getFilebase(fileid);
	   if(filebase!=null){
		fileorder.setOrderFileid(filebase.getFileId());
		fileorder.setOrderFilename(filebase.getFileName());
		fileorder.setOrderPages("0");
		fileOrderDAO.saveFile0rder(fileorder);
		car.setCarTotalprice(0.0);
		car.setCarPagesum(car.getCarPagesum()+1);
		carDAO.updateCar(car);
		filebase.setFilePrintsum(filebase.getFilePrintsum()+1);
		filebaseDAO.updateFilebase(filebase);
		}
		return car;
	}
	@Override
	public List browseFileState(Member member, Car car) {
		// TODO Auto-generated method stub
		return this.fileOrderDAO.browseFileOrderByCarId(car.getCarId());
	}
	@Override
	public List browseFiles(Member member) {
		// TODO Auto-generated method stub
		return filebaseDAO.browseFilebaseByMemberFile(member);
	}
	@Override
	public List browseImages(Member member) {
		// TODO Auto-generated method stub
		return filebaseDAO.browseFilebaseByMemberImage(member);
	}
	@Override
	public List browseOrderCar(Member member) {
		// TODO Auto-generated method stub
		return carDAO.browseCarBymemberId(member.getMemberId());
	}
	@Override
	public List loadCar(Member member) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
